package org.xml.xpath4js.client;

/* 
xpath4js is a GWT implementation of XPath 2.0 
Copyright (C) 2010  Peter Schonefeld (peter dot schonefeld at gmail)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.util.Iterator;
import java.util.Vector;

public class XPathSequence {

	/**
     * @gwt.typeArgs <com.artbase.xpath.client.XPathItem>
     */	
	Vector _items = new Vector();
	
	public XPathSequence(){};

	/**
     * @gwt.typeArgs <com.artbase.xpath.client.XPathItem>
     */		
	public void SetItems(Vector items) {
		this._items = items;
	}

	/**
     * @gwt.typeArgs <com.artbase.xpath.client.XPathItem>
     */		
	public Vector GetItems() {
		return _items;
	}

	// return child CXPathItem instance at index, i
	public XPathItem GetItemAt(int i){
		return (XPathItem) _items.get(i);
	}; 
	
	public void AppendItem(XPathItem item){
		_items.add(item);
	};
	
	public void Clear(){
		_items.clear();
	}
	
    public void ConcatSequence(XPathSequence other){
    	this._items.addAll(other.GetItems());
    };
    
	//will add a number of sequence access methods.
	public XPathSequence GetAtomicValues(){
		XPathSequence sq = new XPathSequence();
		/**
	     * @gwt.typeArgs <com.artbase.xpath.client.XPathItem>
	     */			
		Iterator it = _items.iterator();
		XPathItem i = null;
		while(it.hasNext()){
			i = (XPathItem) it.next();
			if(i.IsNode()){
				sq.AppendItem(i);
			}
		}
		return sq;		
	};
	
	public XPathSequence GetAtomicValues(String xsdType){
		XPathSequence sq = new XPathSequence();
		/**
	     * @gwt.typeArgs <com.artbase.xpath.client.XPathItem>
	     */	
		Iterator it = _items.iterator();
		XPathItem i = null;
		while(it.hasNext()){
			i = (XPathItem) it.next();
			if(i.IsNode() && i.GetXSDType().compareTo(xsdType)==0){
				sq.AppendItem(i);
			}
		}
		return sq;			
	};
	
	public XPathSequence GetNodesOfType(XPathNodeType nodeType){
		XPathSequence sq = new XPathSequence();
		/**
	     * @gwt.typeArgs <com.artbase.xpath.client.XPathItem>
	     */	
		Iterator it = _items.iterator();
		XPathItem i = null;
		while(it.hasNext()){
			i = (XPathItem) it.next();
			if(i.IsNode()){
				if(((XPathNode)i).GetNodeType()==nodeType){
					sq.AppendItem(i);
				}
			}
		}
		return sq;		
	};
	
	public int GetSize(){
		return this._items.size();
	};
	
	public boolean IsSingleton(){
		return this._items.size()==1;
	};
	
	public boolean IsEmpty(){
		return this._items.size()==0;
	};
	
	void SortItemsByDocOrder(){
		//TODO:
	};

	@Override
	public String toString(){
		String result = "";
		/**
	     * @gwt.typeArgs <com.artbase.xpath.client.XPathItem>
	     */	
		Iterator it = _items.iterator();
		XPathItem i = new XPathItem();
		while(it.hasNext()){
			i = (XPathItem) it.next();
			if(i.IsNode()){
				result += "<"+i.toString()+"> , ";	
			}
			else result += i.toString() + " , ";
		}		
		return result == ""?"nothing":result.substring(0,result.lastIndexOf(','));
	};
	
	public String toHTMLString(){
		String result = "";
		/**
	     * @gwt.typeArgs <com.artbase.xpath.client.XPathItem>
	     */	
		Iterator it = _items.iterator();
		XPathItem i = new XPathItem();
		while(it.hasNext()){
			i = (XPathItem) it.next();
			if(i.IsNode()){
				String s = i.toString();
				if(s.contains("\""))
					result += s+" , ";	
				else
					result += "&lt;"+s+"&gt; , ";	
			}
			else result += i.toString() + " , ";
		}		
		return result == ""?"nothing":result.substring(0,result.lastIndexOf(','));
	};	
	

}
